/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.dbg.view.content {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.dbg.model.content.MLDBGTextModel;
	import com.googlecode.milib.display.service.MLTextField;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.utils.getDefinitionByName;

	/**
	 * @author Marek Brun
	 */
	public class MLDBGTextView extends AbstractMLDBGWindowContent implements IMLBroadcasterListener  {

		private var model:MLDBGTextModel;
		private var mltf:MLTextField;

		public function MLDBGTextView(name:String) {
			var MCTextClass:Class=Class(getDefinitionByName('MCMLDBGWindowContentText'));
			super(new MCTextClass(), name);
			mltf=mlmc.getMLTextField('tf');
			addEventsMapping(mltf);
			mltf.addListener(this);
		}
		
		override protected function doSetupScroller():void {
			scroller.setScrolled(mltf);
		}
		
		override protected function drawData():void {
			var isScrolledToDown:Boolean=mltf.tf.scrollV==mltf.tf.maxScrollV;
			mltf.tf.htmlText=model.getText();
			if(isScrolledToDown){ mltf.tf.scrollV=mltf.tf.maxScrollV; }
		}
		
		override protected function doSize():void {
			var isScrolledToDown:Boolean=mltf.tf.scrollV==mltf.tf.maxScrollV;
			mltf.tf.width=width;			mltf.tf.height=height;
			if(isScrolledToDown){ mltf.tf.scrollV=mltf.tf.maxScrollV; }
		}
		
		override protected function doIsEnabled():void {
			model.addListener(this);
			scroller.setScrolled(mltf);
		}
		
		override protected function doIsDisabled():void {
			model.removeListener(this);
			scroller.unsetScrolled();
		}
		
		public function setModel(model:MLDBGTextModel):void {
			if(this.model){ this.model.removeListener(this); }
			this.model=model;
			if(isEnabled){
				this.model.addListener(this);
				drawData();
			}
			setTitle(model.getText());
		}
		
//********************************************************************************************
//		events for MLDBGTextListView
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case model:
					switch(ev.event) {
						case model.event_TextChanged:
							drawData();
							setTitle(model.getText());
							broadcastEvent(event_TitleChanged);
						break;
						case model.event_AddText:
							setTitle(ev.data);
						break;
					}
				break;
			}
		}
		
	}
}
